require "Net/chatHandler"
------------------已废弃--------------------------
UIChatPanelMini = {};
HelperFunc.SetUIEnv(UIChatPanelMini)
local this = UIChatPanelMini;

this.btn_world = nil;
this.btn_guild = nil;
this.btn_enlist = nil
this.btn_private = nil;
this.btn_close = nil;
this.btn_expand = nil;
this.hasInit = false;
this.msgContent = nil;
this.btnCtrl = nil;
this.curChannel = Config.MsgChannel.World;
this.chatData = {};

function UIChatPanelMini.Init()
    local name, packageName = getUIName(PanelNames.UIChatPanelMini);
    panelMgr:Push(name, packageName, this.onCreate,Config.useLocalUI);
end

--定义控件
function UIChatPanelMini.onCreate(obj)
    if not hasInit then
        hasInit = true;
        btn_world = obj:GetChild("worldBtn");
        btn_guild = obj:GetChild("guildBtn");
        btn_enlist = obj:GetChild("enlistBtn");
        btn_private = obj:GetChild("privateBtn");
        btn_close = obj:GetChild("closeBtn");
        btn_expand = obj:GetChild("expandBtn");
        msgContent = obj:GetChild("msgList");
        btnCtrl = obj:GetController("btnCtrl");
        logicMgr.ChatManager.LoadEmoji()
        this.addEvent();
    end
    this.onClickWorld();
end

function UIChatPanelMini.addEvent()
    btn_world.onClick:Add(this.onClickWorld)
    btn_guild.onClick:Add(this.onClickGuild)
    btn_enlist.onClick:Add(this.onClickEnlist)
    btn_private.onClick:Add(this.onClickPrivate)
    btn_close.onClick:Add(this.closePanel)
    btn_expand.onClick:Add(this.expandPanel)
end

function UIChatPanelMini.removeEvent()
    btn_world.onClick:Remove(this.onClickWorld)
    btn_guild.onClick:Remove(this.onClickGuild)
    btn_enlist.onClick:Remove(this.onClickEnlist)
    btn_private.onClick:Remove(this.onClickPrivate)
    btn_close.onClick:Remove(this.closePanel)
    btn_expand.onClick:Remove(this.expandPanel)
end

function UIChatPanelMini.listernEvent()
    ListenEvent(Config.EventType.Fresh_Chat,this.freshReceiveMsg);
end

function UIChatPanelMini.LocalCloseEvent()
    CloseEvent(Config.EventType.Fresh_Chat,this.freshReceiveMsg);
end

function UIChatPanelMini.OnDestroy()
    this.removeEvent();
end

function UIChatPanelMini.OnEnable()
    logicMgr.ChatManager.LoadAllMsgFromFile()
    this.listernEvent();
end

function UIChatPanelMini.OnDisable()
    this.LocalCloseEvent();
end

function UIChatPanelMini.onClickWorld()
    curChannel = Config.MsgChannel.World;
    btnCtrl.selectedPage = "world";
    this.freshReceiveMsg();
end

function UIChatPanelMini.onClickGuild()
    UISysTips.AddMsg("您还没有公会");
    if curChannel == Config.MsgChannel.World then
        btnCtrl.selectedPage = "world";
    else
        btnCtrl.selectedPage = "private";
    end
    return;
--[[    curChannel = Config.MsgChannel.Guild;
    this.freshReceiveMsg();]]
end

function UIChatPanelMini.onClickEnlist()
    UISysTips.AddMsg("暂未开放");
    if curChannel == Config.MsgChannel.World then
        btnCtrl.selectedPage = "world";
    else
        btnCtrl.selectedPage = "private";
    end
    return;
end

function UIChatPanelMini.onClickPrivate()
    local PrivateList = logicMgr.ChatManager.returnSelfPrivateList();
    if #PrivateList == 0 then
        btnCtrl.selectedPage = "world";
        UISysTips.AddMsg("您还没有聊天好友！")
        return;
    end
    curChannel = Config.MsgChannel.Person;
    this.freshReceiveMsg();
end

function UIChatPanelMini.freshReceiveMsg()
    chatData = logicMgr.ChatManager.returnChatMsg(curChannel)
--[[
    print("==========UIChatPanelMini=================>"..#chatData)
    printTable(chatData);]]

    msgContent.itemRenderer = this.setData;
    msgContent.numItems = #chatData;
    msgContent.scrollPane:ScrollBottom();
end

function UIChatPanelMini.setData(index,obj)
    local item = CtrlManager.GetCtrl(PanelNames.ChatMsgItem);
    if item ~= nil then
        item.initData(obj,chatData[index + 1],true);
    end
end

function UIChatPanelMini.closePanel()
    --Util.SetPlayerPrefs("UICityChatMini",0)
    closeUI(PanelNames.UIChatPanelMini)
    FireEvent(Config.EventType.CityChatOpenMiniBtn);
end

function UIChatPanelMini:expandPanel()
    showUI(PanelNames.UIChatPanel)
end

return UIChatPanelMini;
